package com.beilang.base.codelookup.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Code Lookup Configuration Bean.
 */

public class CodeLookupBean {

    private String codeType;
    private String codeTable;
    private String codeDescCol;
    private String codeIdCol;
    private String orderByCol;
    private String orderByType;    

    // Effective Start End Date Columns
    private String effStartDtCol;
    private String effEndDtCol;

    // Obsolete Column and list of obsolete values
    private String obsoleteCol;
    private List obsoleteValueList;

    // Code lookup filter map
    private Map sqlFilterMap;

    public CodeLookupBean() {
        obsoleteValueList = new ArrayList();
        sqlFilterMap = new HashMap();
    }

    public String getCodeType() {
        return codeType;
    }

    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }

    public String getCodeTable() {
        return codeTable;
    }

    public void setCodeTable(String codeTable) {
        this.codeTable = codeTable;
    }

    public String getCodeDescCol() {
        return codeDescCol;
    }

    public void setCodeDescCol(String codeDescCol) {
        this.codeDescCol = codeDescCol;
    }

    /**
     * Return code description column name.
     * eg. if codeDescCol = "COL1 || COL2 AS DescCol"
     *     then this method will return DescCol
     * 
     * @return code description column name.
     */
    public String parseCodeDescCol() {
    	String colName = null;
    	if (codeDescCol != null) {
    		String[] result = codeDescCol.trim().split("\\s");
    		if (result != null && result.length > 0) {
    			colName = result[result.length-1];
    		}
    	}
    	return colName;
    }
    
    public String getCodeIdCol() {
        return codeIdCol;
    }

    public void setCodeIdCol(String codeIdCol) {
        this.codeIdCol = codeIdCol;
    }

    public String getEffStartDtCol() {
        return effStartDtCol;
    }

    public void setEffStartDtCol(String effStartDtCol) {
        this.effStartDtCol = effStartDtCol;
    }

    public String getEffEndDtCol() {
        return effEndDtCol;
    }

    public void setEffEndDtCol(String effEndDtCol) {
        this.effEndDtCol = effEndDtCol;
    }

    public String getObsoleteCol() {
        return obsoleteCol;
    }

    public void setObsoleteCol(String obsoleteCol) {
        this.obsoleteCol = obsoleteCol;
    }

    public void addObsoleteValue(String obsoleteValue) {
        obsoleteValueList.add(obsoleteValue);
    }

    public List getObsoleteValueList() {
        return obsoleteValueList;
    }

    public void addSqlFilter(String filterName, String filterStr) {
        sqlFilterMap.put(filterName, filterStr);
    }

    public String getSqlFilter(String filterName) {
        return (String) sqlFilterMap.get(filterName);
    }

    public String getOrderByCol() {
        return orderByCol;
    }

    public void setOrderByCol(String orderByCol) {
        this.orderByCol = orderByCol;
    }

    public String getOrderByType() {
        return orderByType;
    }

    public void setOrderByType(String orderByType) {
        this.orderByType = orderByType;
    }

    public String toString() {
    	StringBuffer buffer = new StringBuffer();
		buffer.append("codeType = [");
		buffer.append(codeType);
		buffer.append("] codeTable = [");  
		buffer.append(codeTable);
		buffer.append("] codeDescCol = [");  
		buffer.append(codeDescCol);
		buffer.append("] codeIdCol = [");
		buffer.append(codeIdCol);
		buffer.append("] effStartDtCol = [");
		buffer.append(effStartDtCol);
		buffer.append("] effEndDtCol = [");
		buffer.append(effEndDtCol);
		buffer.append("] obsoleteCol = [");
		buffer.append(obsoleteCol);
		buffer.append("] obsoleteValueList: [");
			
		for ( int i = 0; i < obsoleteValueList.size(); i++ ) {
			buffer.append("] ");
			buffer.append(i);
			buffer.append(" - ");
			buffer.append(obsoleteValueList.get(i));
		}
		buffer.append("] sqlFilterMap: [");
		
		Set keySet = sqlFilterMap.keySet();
		if (keySet != null) {
			Iterator keys = keySet.iterator();
			while( keys.hasNext()) {
				String key = (String)keys.next();
				buffer.append(" key: ");
				buffer.append(key);
				buffer.append(" - ");
				String value = (String)sqlFilterMap.get(key);
				buffer.append(value);
				buffer.append("]");
			}
		}
		buffer.append("\n");
		return buffer.toString();
	}
}
